import java.util.*;

public class TestBinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;//就是根节点

    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");

    }

    //获取整个树的节点个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int size = size(root.right) + size(root.left) + 1;
        return size;
    }

    public static int nodeSize;

    //利用遍历的思路求size
    public void size2(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        size2(root.right);
        size2(root.left);
    }
/*    public  int getLeafNodeCount(TreeNode root){
        if (root==null){
            return 0;
        }
        if (root.right==null&&root.left==null){
            return 1;
        }
        return getLeafNodeCount(root.right)+getLeafNodeCount2(root.left);
    }

   public static int count;
    //求叶子节点的个数  遍历思路
    public  int getLeafNodeCount2(TreeNode root){
        if (root==null){
            return 0;
        }
        if (root.right==null&&root.left==null){
            count++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
        return count;
    }*/

    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            return 1;
        }
        return getLeafNodeCount(root.right) + getLeafNodeCount(root.left);
    }

    public int leafSize;

    public void getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return;
        }

        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    //计算第k层有多少个节点 已知前提：k是合法的
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    //求树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight((root.left));
        int rightHeight = getHeight(root.right);

        return Math.max(leftHeight, rightHeight) + 1;
    }

    //找到
    public TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode ret = find(root.left, val);
        if (ret != null) {
            return ret;
        }
        ret = find(root.right, val);
        if (ret != null) {
            return ret;
        }
        return null;
    }

    //判断两颗相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //一个为空，一个不为空
        if (p != null && q == null || p == null && q != null) {
            return false;
        }
        //此时要么两个都为空，要么都不为空
        if (p == null && q == null) {
            return true;
        }
        //都不为空
        if (p.val != q.val) {
            return false;
        }
        //此时两个都不为空，val值也一样，说明根节点相同
        //判断左右树是否相同
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);

    }

    //判断子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        //因为root要递归，递归到后面root可能为空
        if (root == null) {
            return false;
        }
        //两颗树相同时，成立
        if (isSameTree(root, subRoot)) {
            return true;
        }
        //判断root的左子树和subRoot
        if (isSubtree(root.left, subRoot)) {
            return true;
        }
        //判断root的右子树和subRoot
        if (isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;

    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        //代码优化部分******减少一些递归和交换的次数
        if (root.left == null && root.right == null) {
            return root;
        }
        //          ******
        TreeNode ret = root.left;
        root.left = root.right;
        root.right = ret;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //判断平衡二叉树  ===》所有节点的左右子树的深度差必须<=1
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);

        return Math.abs(leftH - rightH) <= 1
                && isBalanced(root.left)
                && isBalanced(root.right);
    }

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return leftH > rightH ? leftH + 1 : rightH + 1;
    }

    //判断平衡二叉树 第二种解法 优化 让时间复杂度为O(N)
    public boolean isBalanced2(TreeNode root) {
        if (root == null) {
            return true;
        }
        return maxDepth2(root) >= 1;

    }

    public int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = maxDepth2(root.left);
        if (leftDepth < 0) {
            return -1;
        }
        int rightDepth = maxDepth2(root.right);
        if (rightDepth < 0) {
            return -1;
        }
        if (Math.abs(leftDepth - rightDepth) <= 1) {
            return Math.max(leftDepth, rightDepth) + 1;
        } else {
            return -1;
        }
    }

    //mxaxDepth2的另一种写法
    public int maxDepth3(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = maxDepth3(root.left);
     /*   if(leftDepth<0){
            return -1;
        }*/
        int rightDepth = maxDepth3(root.right);
       /* if(rightDepth<0){
            return -1;
        }*/
        if (leftDepth > 0
                && rightDepth > 0
                && Math.abs(leftDepth - rightDepth) <= 1) {
            return Math.max(leftDepth, rightDepth) + 1;
        } else {
            return -1;
        }
    }

    //判断对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricChild(root.left, root.right);
    }

    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {
        //结构不一样,一个为空,一个不为空
        if (leftTree != null && rightTree == null || leftTree == null && rightTree != null) {
            return false;
        }
        //两个都为空
        if (leftTree == null && rightTree == null) {
            return true;
        }
        //两个都不为空  值不一样
        if (leftTree.val != rightTree.val) {
            return false;
        }
        //最后要满足 左子树的左 和右子树的右 对称 ;以及 左子树的右 和 右子树的左 对称
        return isSymmetricChild(leftTree.left, rightTree.right)
                && isSymmetricChild(leftTree.right, rightTree.left);
    }

    //使用队列实现 层序遍历
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    //层序遍历2
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }

        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while (size > 0) {
                TreeNode cur = queue.poll();
//                list.add(cur);//这里报错是因为返回值有问题,但是在OJ里跑是没问题的
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                    size--;
                }
            }
            ret.add(list);
        }
        return ret;
    }

    //完全二叉树的判断
    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return true;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur == null) {
                break;//当遇到null时,直接跳出循环
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while (!queue.isEmpty()) {
            TreeNode node = queue.peek();
            if (node != null) {
                return false;
            } else {
                queue.poll();
            }
        }
        return true;
    }

    //寻找公共祖先
    public TreeNode lowestCommonAncestor
    (TreeNode root, TreeNode p, TreeNode q) {
        if(root==null){
            return null;
        }
//p或者q==root
        if(p==root||q==root){
            return root;
        }
        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);
//两边都不为空
        if(leftTree!=null&&rightTree!=null){
            return root;
//左边为空
        }else if(leftTree!=null){
            return leftTree;

//右边为空
        }else{
            return rightTree;
        }
    }

    public TreeNode lowestCommonAncestor2
            (TreeNode root,TreeNode p,TreeNode q){
        if(root==null){
            return root;
        }
        Stack<TreeNode> stackP=new Stack<>();
        getPath(root,p,stackP);
        Stack<TreeNode> stackQ=new Stack<>();
        getPath(root,q,stackQ);

        //上面代码执行完成,对应的栈存储了指定路径的节点
        int sizeP=stackP.size();
        int sizeQ=stackQ.size();
        if(sizeP>sizeQ){//栈P和Q谁的 元素多谁弹出,直到两者长度相等
            int size=sizeP-sizeQ;
            while(size!=0){
                stackP.pop();
                size--;
            }
        }else{
            int size=sizeQ-sizeP;
            while(size!=0){
                stackQ.pop();
                size--;
            }
        }
        while(!stackP.isEmpty() && !stackQ.isEmpty()){
            if(stackP.peek()==stackQ.peek()){
                return stackP.peek();
            }else{
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }
    //将
    public boolean getPath(TreeNode root, TreeNode node ,Stack<TreeNode> stack){
        if(root==null){
            return false;
        }
        stack.push(root);
        if(root==node){
            return true;
        }
        boolean fla=getPath(root.left,node,stack);
        if(fla){
            return true;
        }
        boolean fla2=getPath(root.right,node,stack);
        if(fla2){
            return true;
        }
        stack.pop();
        return false;
    }

    public void preOrderNot(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;

        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            cur=top.right;
        }
    }

    public void inOrderNot(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;

        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }
    }

    public void postOrderNot(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;

        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.peek();
            if(top.right==null||top.right==prev){
                stack.pop();
                System.out.print(top.val+" ");
                prev=top;//记录当前打印的是谁
            }else{
                cur=top.right;
            }
        }
    }
}


